package function;

import View.CreateGroupWindow;
import View.MainWindow;
import View.SelectGroupHeadWindow;
import View.window;
import client.ClientManager;
import instruction.Group;
import instruction.Member;
import instruction.Message;
import instruction.User;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Circle;

import java.util.ArrayList;
import java.util.regex.Pattern;


public class CreateGroupFunction {

    private static String imagePath="/Picture/headPortrait/复仇者联盟.jpg";
    public static Object $(window window, String id) {
        return (Object) window.getRoot().lookup("#" + id);
    }
    public static void createGroupExec(MainWindow mainWindow, CreateGroupWindow createGroupWindow, SelectGroupHeadWindow selectGroupHeadWindow,ClientManager clientManager){
        ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
        ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
        ((Button)$(createGroupWindow,"goButton")).setOnAction(event->{
            String correctAct = "^[0-9,a-z,A-Z,\\u4e00-\\u9fa5]{1,15}$";
            String groupAccount=((TextField)$(createGroupWindow,"accountText")).getText();
            String groupName=((TextField)$(createGroupWindow,"nameText")).getText();
            String groupIntroduction=((TextArea)$(createGroupWindow,"introductionText")).getText();
            if(groupAccount.equals("")||groupName.equals("")){
                if(groupAccount.equals("")){
                    ((Label)$(createGroupWindow,"accountTip")).setText("账号不能为空");
                }
                if(groupName.equals("")){
                    ((Label)$(createGroupWindow,"nameTip")).setText("群名不能为空");
                }
            }else if(!Pattern.matches(correctAct, groupAccount)){
                ((Label)$(createGroupWindow,"accountTip")).setText("账号是长度不超过15位的中文和英文和数字");
            }else{
                User user=mainWindow.getUserData().getUser();
                String userName=user.getName();
                String userAccount=user.getAccount();
                String userImage=user.getHead();
                String userNickname=user.getNickname();
                String userAge=user.getAge();
                String userSex=user.getSex();
                String userBirthday=user.getBirthday();
                String userSign=user.getSignature();
                String userPhone=user.getPhone();
                String userEmail=user.getEmail();
                String userAddress=user.getAddress();
                Member userMember=new Member(userName,userAccount,userSex,userBirthday,userSign,userEmail,userAge,userAddress,userNickname,userPhone,userImage);
                ArrayList<Member> memberList=new ArrayList<>();
                memberList.add(userMember);
                Group group=new Group(imagePath,groupName,groupAccount,groupIntroduction,userAccount,memberList);
                Message message=new Message("createGroup");
                message.setCreateGroup(group);
                clientManager.getSend().send(message);
                createGroupWindow.close();
            }
        });
        ((Button)$(createGroupWindow,"getAccountButton")).setOnAction(event->{
            ((TextField)$(createGroupWindow,"accountText")).setText(function.account_maker.account_maker());
        });
        ((Button)$(createGroupWindow,"selectButton")).setOnAction(event->{
            selectGroupHeadWindow.InitHead();
            selectGroupHeadWindow.show();
            ((Button)$(selectGroupHeadWindow,"dieButton")).setOnAction(actionEvent -> {
                imagePath="/Picture/headPortrait/死神.jpg";
                ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
                ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
                selectGroupHeadWindow.hide();
            });
            ((Button)$(selectGroupHeadWindow,"bearButton")).setOnAction(actionEvent -> {
                imagePath="/Picture/headPortrait/熊出没.jpg";
                ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
                ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
                selectGroupHeadWindow.hide();
            });
            ((Button)$(selectGroupHeadWindow,"seaButton")).setOnAction(actionEvent -> {
                imagePath="/Picture/headPortrait/海贼王.jpg";
                ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
                ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
                selectGroupHeadWindow.hide();
            });
            ((Button)$(selectGroupHeadWindow,"superButton")).setOnAction(actionEvent -> {
                imagePath="/Picture/headPortrait/超兽武装.jpg";
                ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
                ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
                selectGroupHeadWindow.hide();
            });
            ((Button)$(selectGroupHeadWindow,"fireButton")).setOnAction(actionEvent -> {
                imagePath="/Picture/headPortrait/火影忍者.jpg";
                ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
                ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
                selectGroupHeadWindow.hide();
            });
            ((Button)$(selectGroupHeadWindow,"basButton")).setOnAction(actionEvent -> {
                imagePath="/Picture/headPortrait/灌篮高手.jpg";
                ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
                ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
                selectGroupHeadWindow.hide();
            });
            ((Button)$(selectGroupHeadWindow,"qinButton")).setOnAction(actionEvent -> {
                imagePath="/Picture/headPortrait/秦时明月.jpg";
                ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
                ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
                selectGroupHeadWindow.hide();
            });
            ((Button)$(selectGroupHeadWindow,"avengerButton")).setOnAction(actionEvent -> {
                imagePath="/Picture/headPortrait/复仇者联盟.jpg";
                ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
                ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
                selectGroupHeadWindow.hide();
            });
            ((Button)$(selectGroupHeadWindow,"sheepButton")).setOnAction(actionEvent -> {
                imagePath="/Picture/headPortrait/喜羊羊与灰太狼.jpg";
                ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
                ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
                selectGroupHeadWindow.hide();
            });
            ((Button)$(selectGroupHeadWindow,"detectiveButton")).setOnAction(actionEvent -> {
                imagePath="/Picture/headPortrait/名侦探柯南.jpg";
                ((ImageView)$(createGroupWindow,"groupImage")).setImage(new Image(imagePath,80,80,false,false));
                ((ImageView)$(createGroupWindow,"groupImage")).setClip(getCircle());
                selectGroupHeadWindow.hide();
            });
        });

    }
    public static Circle getCircle(){
        Circle circle = new Circle();
        circle.setRadius(40);
        circle.setCenterX(40);
        circle.setCenterY(40);
        return circle;
    }
}
